package com.syni.mdd.sjb.common.datatreaure.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankApplication;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.entity.DankMerchantsApplication;
import com.syni.mdd.sjb.common.component.mybatis.dxshapp.mapper.DankMerchantsApplicationMapper;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.datatreaure.service.DankApplicationService;
import com.syni.mdd.sjb.common.datatreaure.service.DankMerchantsApplicationService;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rp.dataBankBusiness.MerchantApplicationVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-09-19
 */
@Service
public class DankMerchantsApplicationServiceImpl extends ServiceImpl<DankMerchantsApplicationMapper, DankMerchantsApplication> implements DankMerchantsApplicationService {
    @Autowired
    DankMerchantsApplicationMapper dankMerchantsApplicationMapper;

    @Autowired
    DankMerchantsApplicationService dankMerchantsApplicationService;

    @Autowired
    DankApplicationService dankApplicationService;

    @Value("${dxsh.domain}")
    private String domain;

    @Override
    public List<DankMerchantsApplication> getHomePageService(Integer userId) {
        Page<DankMerchantsApplication> page = new Page(1,8);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DankMerchantsApplication.USER_ID,userId);
        return dankMerchantsApplicationMapper.selectPage(page,queryWrapper).getRecords();
    }

    @Override
    public List<MerchantApplicationVo> getMerchantApp(Integer userId) {
        List<MerchantApplicationVo> merchantApplicationVos = new ArrayList<>();
        List<DankMerchantsApplication> homePageService = dankMerchantsApplicationService.getHomePageService(userId);
        if(CollectionUtils.isNotEmpty(homePageService)){
            String appId = homePageService.get(0).getAppId();
            List<Integer> appIds = JSON.parseArray(appId, Integer.class);
            if(appIds.size() != 0) {
                List<DankApplication> appByIds = dankApplicationService.getAppByIds(appIds);
                merchantApplicationVos = BeanUtils.copyList(appByIds, MerchantApplicationVo.class);
            }
        }

        //从数据库读取
        MerchantApplicationVo merchantApplicationVo1 = new MerchantApplicationVo();
        merchantApplicationVo1.setAppName("服务市场").setClickUrl("syni://serviceHome").setIcon("https://dsp-1251624226.cos.ap-guangzhou.myqcloud.com/dsp/20200929144832209.png");
        merchantApplicationVos.add(0,merchantApplicationVo1);
        return merchantApplicationVos;
    }

    @Override
    public DankMerchantsApplication findByUserId(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(DankMerchantsApplication.USER_ID,userId);
        return dankMerchantsApplicationMapper.selectOne(queryWrapper);
    }


}
